import axios from 'axios'
import $$ from './statusCode.config'
const httpAxios9090 = {}
httpAxios9090.install = function (Vue) {
  const instance = axios.create({
    baseURL: 'http://' + $$.heIp + ':9090/'
  })
  instance.interceptors.request.use(
    function (config) {
      if (sessionStorage.getItem('accessToken')) {
        config.headers.authorization = sessionStorage.getItem('accessToken')
      }
      return config
    },
    function (error) {
      return Promise.reject(error)
    }
  )

  instance.interceptors.response.use(function (response) {
    if (response.data.statusCode === 70000) {
      const token = JSON.parse(response.headers.authorization)
      sessionStorage.setItem('managerNickName', token.manager.nickName)
      sessionStorage.setItem('accessToken', token.accessToken)
      sessionStorage.setItem('refreshToken', token.refreshToken)
      const managerID = token.manager.id
      sessionStorage.setItem('id', managerID)
    }
    return response
  })

  Vue.prototype.$http9090 = instance
}
// export default httpHelper
export default httpAxios9090
